// Agent gatekeeper in project vacaloucaMASSim2010

/* Initial beliefs and rules */

/* Initial goals */

!search_switch_begin.

/* Plans */

+!search_switch_begin : not pos(_,_,_)
<- .print("waiting position...");
	.wait("+pos(_,_,_)");
	.print("got position...");
	!!search_switch_begin.

+!search_switch_end : not pos(_,_,_)
<- .print("waiting position...");
	.wait("+pos(_,_,_)");
	.print("got position...");
	!!search_switch_end.
	
+!move_to_switch : not pos(_,_,_)
	<- .print("waiting position...");
	.wait("+pos(_,_,_)");
	.print("got position...");
	!!move_to_switch.
	
+!search_switch_begin : not switch(_,_,_) & pos(X,Y,_) & not actions.fence_begin(X,Y,Xf,Yf) 
	<- .print("gatekeeper: ainda nao achei o switch, Indo pro comeco da cerca...");
		actions.astar(X,Y,Xf,Yf,Direction,0);
		moveTo(Direction);
		!!search_switch_begin.

+!search_switch_begin : not switch(_,_,_) & pos(X,Y,_) & actions.fence_begin(X,Y,Xf,Yf) 
	<- .print("gatekeeper: ainda nao achei o switch, Indo pro FIM da cerca...");
		actions.astar(X,Y,Xf,Yf,Direction,0);
		moveTo(Direction);
		!search_switch_end.

+!search_switch_begin : switch(_,_,_)
	<- .print("gatekeeper: ja tenho switch, indo pra la...");
		!move_to_switch.

+!search_switch_end : not switch(_,_,_) & pos(X,Y,_) & not actions.fence_end(X,Y,Xf,Yf) 
	<- .print("gatekeeper: ainda nao achei o switch, Indo pro FIM da cerca...");
		actions.astar(X,Y,Xf,Yf,Direction,0);
		moveTo(Direction);
		!!search_switch_end.

		
+!search_switch_end : switch(_,_,_) & pos(_,_,_)
	<- actions.debug_felipe("Achei e to indo pra la");
		.print("gatekeeper: ja tenho switch, indo pra la...");
		!move_to_switch.
		



+!move_to_switch : pos(X,Y,_) & switch(_,Xs,Ys)  & not actions.afastado_n_cells(2,X,Y,Xs,Ys)
	<- .print("gatekeeper: Movendo para o switch...");
		actions.astar(X,Y,Xs,Ys,Direction,0);
		//actions.debug_felipe("Movendo para switch -> ", Direction);
		moveTo(Direction);
		//actions.debug_felipe("Movi -------------> ", Direction);
		!!move_to_switch.

+!move_to_switch : pos(X,Y,_) & switch(_,Xs,Ys)  & actions.afastado_n_cells(2,X,Y,Xs,Ys)
<- 	actions.debug_felipe("To no Switch");
	.print("gatekeeper: Estou posicionado no switch, aguardando comandos...");
	do(skip);
	!gatekeeper_wait_command.
	
+!gatekeeper_wait_command : pos(X,Y,_) & switch(_,Xs,Ys) & actions.afastado_n_cells(2,X,Y,Xs,Ys) & not open(fence)
<- .print("gatekeeper: Aguardando ordens no switch...");
	actions.debug_felipe("Aguardando Ordens...", Direction);
	do(skip);
	!!gatekeeper_wait_command.

/* +!gatekeeper_wait_command : open(fence)
<- actions.debug_felipe("gatekeeper: Abrindo a cerca!").*/

+!gatekeeper_wait_command : pos(X,Y,_) & switch(_,Xs,Ys) & actions.afastado_n_cells(2,X,Y,Xs,Ys) & open(fence)
<- 	actions.debug_felipe("gatekeeper: Abrindo a cerca!");
	.print("gatekeeper: Abrindo a cerca...");
	actions.astar(X,Y,Xs,Ys,Direction,1); // Abrindo
	.print("gatekeeper: Abrindo a cerca...");
	moveTo(Direction);
	actions.debug_felipe("deveria ter aberto");
	actions.astar(Xs,Ys,X,Y,Direction,1); //Voltando
	moveTo(Direction);
	actions.debug_felipe("deveria ter fechado");
	actions.astar(X,Y,Xs,Ys,Direction,1); // Abrindo
	moveTo(Direction);
	actions.debug_felipe("deveria ter aberto!");
	!!gatekeeper_wait_command.

